package com.hntxk.cqzd.consultation.action;


import com.hntxk.core.util.StringUtil;
import com.hntxk.cqzd.common.action.CqzdBaseController;
import com.hntxk.cqzd.common.service.CommonFileService;
import com.hntxk.cqzd.consultation.entity.TblDibFile;
import com.hntxk.cqzd.consultation.service.DibFileService;
import com.hntxk.cqzd.consultation.service.impl.DibFileServiceImpl;
import com.hntxk.cqzd.diacrisis.service.impl.DiacrisisInfoServiceImpl;
import com.hntxk.cqzd.flup.entity.FlupFile;
import com.hntxk.cqzd.flup.service.FlupFileService;
import com.hntxk.cqzd.flup.service.impl.FlupFileServiceImpl;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;


@Controller
@RequestMapping("cqzd/do/dibfile")
@Slf4j
public class DibFileController extends CqzdBaseController
{


    /** 查询页面的path */

    private final static String LIST_JSP_PATH = "cqzd/flup/flupFile_list";

    private final DibFileService dibFileService;



    @Autowired
    public DibFileController(@Qualifier("dibFileManageService") DibFileService dibFileService) {
        super(dibFileService, LogManager.getLogger(DibFileController.class));
        this.dibFileService = dibFileService;
    }


    /**
     *
     * @return 患者基本信息主页
     */
    @ApiIgnore
    @GetMapping(value = "/index")
    public String index() {
        return LIST_JSP_PATH;
    }

    /**
     * 查询基础信息
     * @param model
     * @param request
     * @return
     */
    @ApiOperation("查询基础信息")
    @RequestMapping(value = "/queryList",method = {RequestMethod.GET,RequestMethod.POST}, produces= PRODUCE_UTF8_JSON)
    @ResponseBody
    public String queryFlupFile(@ModelAttribute TblDibFile model, HttpServletRequest request) {

        try {
            //设置分页信息
            model.setPagination(request);
            return toJson(model, dibFileService.query2Json(DibFileServiceImpl.DIBFILE_MAPPER_NAMESPACE + "query", model, true));
        } catch (Exception e) {

            return getError(e);
        }
    }

    //根据文件代号下载文件
    @ApiImplicitParam(name = "fileCode", value = "文件Code",example = "abcde",required = true, dataTypeClass = String.class)
    @GetMapping(value = "getFileByCode/{fileCode}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public ResponseEntity<byte[]> getFile(@PathVariable("fileCode") String fileCode) {
        return dibFileService.getFile(fileCode);
    }

    @RequestMapping(value = "getFileByFkId/{id}",produces = PRODUCE_UTF8_JSON)
    @ResponseBody
    public String getFileByFkId(@PathVariable String id){
        Long flupId=Long.parseLong(id);
        return toJson(dibFileService.getByFkId(flupId));
    }






}
